package com.hxkj.log;

import com.hxkj.domain.Constant.CmsConstants;
import com.hxkj.domain.po.*;
import com.hxkj.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/***
 * @desc 自定义cms日志切面类
 * @author yaosy
 * @date 2018年9月9日14:11:32
 * **/
@Component
@Aspect
@Slf4j
public class CmsLogInterceptService {

    @Autowired
    private LogService logService;


    @Before("within(com.hxkj.MQ..*)&&@annotation(cmsLoggerManage)")
    public void beforeLogger(JoinPoint joinPoint,CmsLoggerManage cmsLoggerManage){
        LocalDateTime localDateTime =  LocalDateTime.now();
        log.info(localDateTime.toString()+"执行["+cmsLoggerManage.log()+"]开始");
    }


    @After("within(com.hxkj.MQ..*)&&@annotation(cmsLoggerManage)")
    public void afterReturingLogger(JoinPoint joinPoint,CmsLoggerManage cmsLoggerManage){
        Object obj[] =  joinPoint.getArgs();
        String params = parseParams(obj);
        log.info("方法切入前操作!!!"+params);
    }


    @AfterThrowing(value = "within(com.hxkj.MQ..*) &&@annotation(cmsLoggerManage)", throwing = "ex")
    public void afterThrowingLogger(JoinPoint joinPoint,CmsLoggerManage cmsLoggerManage, Exception ex){
        log.info("异常处理!!!");
    }


    protected String parseParams(Object[] params){
        if(params == null && params.length ==0) return "";
        StringBuffer sb = new StringBuffer("传入参数个数#个");
        Log log = new Log();
        for(Object obj : params){
            log = new Log();
            if(obj instanceof Column){//分类
                Column column = (Column)obj;
                log.setSiteId(column.getSiteId());
                log.setName(column.getColumnName());
                log.setMajorId(column.getColumnId());
                log.setType(CmsConstants.COLUMNLIST);
                this.saveLog(log);
            }else if(obj instanceof Channel){//频道
                Channel channel = (Channel)obj;
                log.setSiteId(channel.getSiteId());
                log.setName(channel.getChannelName());
                log.setType(CmsConstants.CHANNEL);
                log.setMajorId(channel.getChannelId());
                this.saveLog(log);
            }else if(obj instanceof Leaflet){//单页
                Leaflet leaflet = (Leaflet)obj;
                log.setType(CmsConstants.LEAFLET);
                log.setName(leaflet.getHeadline());
                log.setMajorId(leaflet.getLeafletId());
                log.setSiteId(leaflet.getSiteId());
                this.saveLog(log);
            }else if(obj instanceof Tags){//tags标签
                Tags tags = (Tags) obj;
                log.setSiteId(tags.getSiteId());
                log.setMajorId(tags.getTagsId());
                log.setName(tags.getTagsName());
                log.setType(CmsConstants.TAGSDEATIL);
                this.saveLog(log);
            }else if(obj instanceof Article){//文章
                Article article = (Article)obj;
                log.setType(CmsConstants.ARTICLEDETAIL);
                log.setName(article.getHeadline());
                log.setMajorId(article.getArticleId());
                log.setSiteId(article.getSiteId());
                this.saveLog(log);
            }
        }
        return sb.toString();
    }


    protected void saveLog(Log log){
        logService.save(log);
    }

}
